package com.ymatou.iapi.email.testcase;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.mongodb.util.Hash;
import com.ymatou.iapi.email.parameter.SendEmailBean;
import com.ymatou.iapi.email.service.SendEmailCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.database.model.MailContent;
import com.ymttest.database.model.MailRecord;
import com.ymttest.database.sqlwapper.EmailWapper;
import com.ymttest.utils.TestDataManager;

/**
 * 发送邮件 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_SendEmail {
	private static SendEmailBean sendemailBean;
	private static SendEmailCall sendemailCall;
	private static EmailWapper emailwapper;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("发送邮件");
		emailwapper = new EmailWapper();
	}

	@Before
	public void caseUp() {
		sendemailBean = new SendEmailBean();
		sendemailCall = new SendEmailCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(sendemailCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SendEmail_001() {
		Logger.start(true, "发送邮件成功");
		try {
			sendemailBean.setAppId("AutoTest");
			sendemailBean.setContent("测试");
			// 测试账号 配置在mail_sender 表中
			sendemailBean.setFromMail("alarm@ymatou.com");
			sendemailBean.setFromName("邮件测试AutoTest");
			sendemailBean.setSubject("subject");
			sendemailBean.setToMail("zhouyi@ymatou.com");
			sendemailCall.setData(sendemailBean);
			sendemailCall.callService();
			Logger.verifyEquals("success", sendemailCall.getString("errorMessage"), "验证errorMessage");
			Logger.verifyEquals("true", sendemailCall.getString("success"), "验证success");
			Logger.verifyEquals("0", sendemailCall.getString("errorCode"), "验证errorCode");

			JsonArray idList = sendemailCall.getJsonObject().getAsJsonObject("result").getAsJsonArray("idList");
			Logger.verifyEquals(1, idList.size(), "验证条数");
			Logger.verifyNotNull(sendemailCall.getJsonObject().getAsJsonObject("result").get("batchId").getAsString(), "验证batchId");
			for (JsonElement je : idList) {
				MailRecord record = emailwapper.selectMailRecordById(je.getAsString()).get(0);
				Logger.verifyEquals(record.getAppId(), sendemailBean.getAppId(), "验证appid");

				Logger.verifyEquals(record.getFromMail(), sendemailBean.getFromMail(), "验证frommail");
				Logger.verifyEquals(record.getToMail(), sendemailBean.getToMail(), "验证ToMail");
				Logger.verifyEquals("default", record.getMailTemplateId(), "验证MailTemplateId");
				// 0 ready 1 success
				Logger.verifyEquals(true, record.getStatus() == 0 || record.getStatus() == 1, "验证Status为0或1");
				MailContent content = emailwapper.selectMailContentById(je.getAsString()).get(0);
				Logger.verifyEquals(content.getContent(), sendemailBean.getContent(), "验证Content");
				Logger.verifyEquals(content.getSubject(), sendemailBean.getSubject(), "验证Subject");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_SendEmail_002() {
		Logger.start(true, "发送多个邮件成功");
		try {
			sendemailBean.setAppId("AutoTest");
			sendemailBean.setContent("测试");
			// 测试账号 配置在mail_sender 表中
			sendemailBean.setFromMail("alarm@ymatou.com");
			sendemailBean.setFromName("邮件测试AutoTest");
			sendemailBean.setSubject("subject");
			sendemailBean.setToMail("zhouyi@ymatou.com;zhangxiaoming@ymatou.com");
			sendemailCall.setData(sendemailBean);
			sendemailCall.callService();
			Logger.verifyEquals("success", sendemailCall.getString("errorMessage"), "验证errorMessage");
			Logger.verifyEquals("true", sendemailCall.getString("success"), "验证success");
			Logger.verifyEquals("0", sendemailCall.getString("errorCode"), "验证errorCode");

			JsonArray idList = sendemailCall.getJsonObject().getAsJsonObject("result").getAsJsonArray("idList");

			Logger.verifyNotNull(sendemailCall.getJsonObject().getAsJsonObject("result").get("batchId").getAsString(), "验证batchId");

			String[] tomails = sendemailBean.getToMail().split(";");
			Logger.verifyEquals(tomails.length, idList.size(), "验证条数");

			List<String> mtomails = new ArrayList<String>();
			for (String string : tomails) {
				mtomails.add(string);
			}

			for (JsonElement je : idList) {
				MailRecord record = emailwapper.selectMailRecordById(je.getAsString()).get(0);
				Logger.verifyEquals(record.getAppId(), sendemailBean.getAppId(), "验证appid");

				Logger.verifyEquals(record.getFromMail(), sendemailBean.getFromMail(), "验证frommail");

				Logger.verifyEquals(true, mtomails.contains(record.getToMail()), "验证ToMail");
				mtomails.remove(sendemailBean.getToMail());
				Logger.verifyEquals("default", record.getMailTemplateId(), "验证MailTemplateId");
				// 0 ready 1 success
				Logger.verifyEquals(true, record.getStatus() == 0 || record.getStatus() == 1, "验证Status为0或1");
				MailContent content = emailwapper.selectMailContentById(je.getAsString()).get(0);
				Logger.verifyEquals(content.getContent(), sendemailBean.getContent(), "验证Content");
				Logger.verifyEquals(content.getSubject(), sendemailBean.getSubject(), "验证Subject");
			}

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_SendEmail_003() {
		Logger.start(true, "发送超过50个邮件失败");
		try {
			sendemailBean.setAppId("AutoTest");
			sendemailBean.setContent("测试");
			// 测试账号 配置在mail_sender 表中
			sendemailBean.setFromMail("alarm@ymatou.com");
			sendemailBean.setFromName("邮件测试AutoTest");
			sendemailBean.setSubject("subject");
			String tomail="";
			for (int i = 0; i <51; i++) {
				tomail=tomail+TestDataManager.getRandomUUID().toString()+"@ymatou.com;";
			}
			sendemailBean.setToMail(tomail);
			sendemailCall.setData(sendemailBean);
			sendemailCall.callService();
			Logger.verifyIsContains("一次发送的邮件数不能超过50个", sendemailCall.getString("errorMessage"), "验证errorMessage");
			Logger.verifyEquals("false", sendemailCall.getString("success"), "验证success");
			Logger.verifyEquals("3", sendemailCall.getString("errorCode"), "验证errorCode");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	@Ignore
	//可以成功
	public void Tc_SendEmail_004() {
		Logger.start(true, "不输入appid失败");
		try {
//			sendemailBean.setAppId("AutoTest");
			sendemailBean.setContent("测试");
			// 测试账号 配置在mail_sender 表中
			sendemailBean.setFromMail("alarm@ymatou.com");
			sendemailBean.setFromName("邮件测试AutoTest");
			sendemailBean.setSubject("subject");
			sendemailBean.setToMail("zhouyi@ymatou.com");
			sendemailCall.setData(sendemailBean);
			sendemailCall.callService();
			Logger.verifyIsContains("appId:may not be null", sendemailCall.getString("errorMessage"), "验证errorMessage");
			Logger.verifyEquals("false", sendemailCall.getString("success"), "验证success");
			Logger.verifyEquals("2", sendemailCall.getString("errorCode"), "验证errorCode");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_SendEmail_005() {
		Logger.start(true, "不输入subject失败");
		try {
			sendemailBean.setAppId("AutoTest");
			sendemailBean.setContent("测试");
			// 测试账号 配置在mail_sender 表中
			sendemailBean.setFromMail("alarm@ymatou.com");
			sendemailBean.setFromName("邮件测试AutoTest");
//			sendemailBean.setSubject("subject");
			sendemailBean.setToMail("zhouyi@ymatou.com");
			sendemailCall.setData(sendemailBean);
			sendemailCall.callService();
			Logger.verifyIsContains("subject:may not be null", sendemailCall.getString("errorMessage"), "验证errorMessage");
			Logger.verifyEquals("false", sendemailCall.getString("success"), "验证success");
			Logger.verifyEquals("2", sendemailCall.getString("errorCode"), "验证errorCode");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_SendEmail_006() {
		Logger.start(true, "不输入content失败");
		try {
			sendemailBean.setAppId("AutoTest");
//			sendemailBean.setContent("测试");
			// 测试账号 配置在mail_sender 表中
			sendemailBean.setFromMail("alarm@ymatou.com");
			sendemailBean.setFromName("邮件测试AutoTest");
			sendemailBean.setSubject("subject");
			sendemailBean.setToMail("zhouyi@ymatou.com");
			sendemailCall.setData(sendemailBean);
			sendemailCall.callService();
			Logger.verifyIsContains("content:may not be null", sendemailCall.getString("errorMessage"), "验证errorMessage");
			Logger.verifyEquals("false", sendemailCall.getString("success"), "验证success");
			Logger.verifyEquals("2", sendemailCall.getString("errorCode"), "验证errorCode");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_SendEmail_007() {
		Logger.start(true, "不输入fromMail失败");
		try {
			sendemailBean.setAppId("AutoTest");
			sendemailBean.setContent("测试");
//			 测试账号 配置在mail_sender 表中
//			sendemailBean.setFromMail("alarm@ymatou.com");
			sendemailBean.setFromName("邮件测试AutoTest");
			sendemailBean.setSubject("subject");
			sendemailBean.setToMail("zhouyi@ymatou.com");
			sendemailCall.setData(sendemailBean);
			sendemailCall.callService();
			Logger.verifyIsContains("fromMail:may not be null", sendemailCall.getString("errorMessage"), "验证errorMessage");
			Logger.verifyEquals("false", sendemailCall.getString("success"), "验证success");
			Logger.verifyEquals("2", sendemailCall.getString("errorCode"), "验证errorCode");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_SendEmail_008() {
		Logger.start(true, "不输入fromName,成功,使用系统配置fromname");
		try {
			sendemailBean.setAppId("AutoTest");
			sendemailBean.setContent("测试");
//			 测试账号 配置在mail_sender 表中
			sendemailBean.setFromMail("alarm@ymatou.com");
//			sendemailBean.setFromName("邮件测试AutoTest");
			sendemailBean.setSubject("subject_no_fromname");
			sendemailBean.setToMail("zhouyi@ymatou.com");
			sendemailCall.setData(sendemailBean);
			sendemailCall.callService();
			Logger.verifyEquals("success", sendemailCall.getString("errorMessage"), "验证errorMessage");
			Logger.verifyEquals("true", sendemailCall.getString("success"), "验证success");
			Logger.verifyEquals("0", sendemailCall.getString("errorCode"), "验证errorCode");

			JsonArray idList = sendemailCall.getJsonObject().getAsJsonObject("result").getAsJsonArray("idList");
			Logger.verifyEquals(1, idList.size(), "验证条数");
			Logger.verifyNotNull(sendemailCall.getJsonObject().getAsJsonObject("result").get("batchId").getAsString(), "验证batchId");
			for (JsonElement je : idList) {
				MailRecord record = emailwapper.selectMailRecordById(je.getAsString()).get(0);
				Logger.verifyEquals(record.getAppId(), sendemailBean.getAppId(), "验证appid");

				Logger.verifyEquals(record.getFromMail(), sendemailBean.getFromMail(), "验证frommail");
				Logger.verifyEquals(record.getToMail(), sendemailBean.getToMail(), "验证ToMail");
				Logger.verifyEquals("default", record.getMailTemplateId(), "验证MailTemplateId");
				// 0 ready 1 success
				Logger.verifyEquals(true, record.getStatus() == 0 || record.getStatus() == 1, "验证Status为0或1");
				MailContent content = emailwapper.selectMailContentById(je.getAsString()).get(0);
				Logger.verifyEquals(content.getContent(), sendemailBean.getContent(), "验证Content");
				Logger.verifyEquals(content.getSubject(), sendemailBean.getSubject(), "验证Subject");
			}

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_SendEmail_009() {
		Logger.start(true, "不输入toMail失败");
		try {
			sendemailBean.setAppId("AutoTest");
			sendemailBean.setContent("测试");
//			 测试账号 配置在mail_sender 表中
			sendemailBean.setFromMail("alarm@ymatou.com");
			sendemailBean.setFromName("邮件测试AutoTest");
			sendemailBean.setSubject("subject");
//			sendemailBean.setToMail("zhouyi@ymatou.com");
			sendemailCall.setData(sendemailBean);
			sendemailCall.callService();
			Logger.verifyIsContains("toMail:may not be null", sendemailCall.getString("errorMessage"), "验证errorMessage");
			Logger.verifyEquals("false", sendemailCall.getString("success"), "验证success");
			Logger.verifyEquals("2", sendemailCall.getString("errorCode"), "验证errorCode");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
}